<!doctype html>
<html lang="en" class="fixed">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>权限管理</title>
    <link rel="apple-touch-icon" sizes="120x120" href="favicon/apple-icon-120x120.png">
    <link rel="icon" type="image/png" sizes="192x192" href="favicon/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="favicon/favicon-16x16.png">
    <link rel="stylesheet" type="text/css"
          href="<?php xcho(loadSource('/assets/plugins/bootstrap/css/bootstrap.min.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/animate.css/animate.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/font-awesome/css/font-awesome.css')) ?>">
    <link rel="stylesheet"
          href="<?php xcho(loadSource('/assets/plugins/data-table/media/css/dataTables.bootstrap.min.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/app/css/style.css')) ?>">
<!--    <link rel="stylesheet" href="--><?php //xcho(loadSource('/app/css/app.css')) ?><!--">-->
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/layui/css/layui.css')) ?>" media="all">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/css/new_main.css')) ?>">
    <style>
        .pagination{
            margin: 0;
        }
    </style>
</head>

<body><?php include_once TPL_PATH . 'public/nav.phtml'; ?>

<div class="wrap">
    <div class="page-body" style="padding: 0">
        <div class="content" style="margin: 0;background: #F4F4F4">
            <div class="content" style="margin: 0;">
                <div class="top_btn from-top-right">
<!--                    <button class="btn btn-wide btn-primary" id="change_levels">批量修改分级</button>-->
<!--                    <button class="btn btn-wide btn-primary"  data-backdrop="false" data-target="#info-modal" id="add">添加</button>-->
                    <button class="layui-btn layui-btn-small layui-btn-warm" id="change_levels"><i class="layui-icon">&#xe642;</i> 批量修改</button>
                    <button class="layui-btn layui-btn-small " id="add"><i class="layui-icon">&#xe608;</i> 添加</button>

                </div>
<!--                <div class="top_pages">-->
<!--                    <p>已选择 2 项</p>-->
<!--                    <ul>-->
<!---->
<!--                    </ul>-->
<!--                </div>-->
                <div class="table table-responsive  layui-form">
                    <table class="layui-table" lay-filter="demo">
                        <thead>
                        <tr>
                            <th><div id="checkAll" ><input  lay-filter="allChoose" type="checkbox" lay-skin="primary" name="checkAll"></div></th>
                            <th>名称</th>
                            <th>分级</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                      <?php foreach ($list as $k =>$v) {?>
                          <tr>
                              <td>
                                  <input  lay-filter="itemChoose" value="<?php xcho($v['id']) ?>" type="checkbox" name="ck" lay-skin="primary"></td>
                              <td>
                                   <?php xcho($v["cn_name"]) ?>
                              </td>
                              <td>
                                  <?php xcho($v["level_value"]) ?>
                              </td>
                              <td><!--编辑 modal-->
                                  <button data-cn_id="<?php   xcho($v['id']) ?>" type="button" class="btn btn-primary edit" data-toggle="modal" data-target="#info-modal">编辑</button>
<!--                                  <button data-cn_id="--><?php //  xcho($v['id']) ?><!--" type="button" class="btn btn-wide btn-warning btn-xs move" data-toggle="modal" data-target="#info-modal">转移</button>-->
                                  <button data-cn_id="<?php   xcho($v['id']) ?>" type="button" class="btn btn-danger delete">删除</button>
                              </td>
                          </tr>
                        <?php } ?>

                        </tbody>
                    </table>
                </div>
<!--            <!--编辑 Modal -->
                <div class="modal fade" id="info-modal" tabindex="-1" role="dialog" aria-labelledby="modal-info-label">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header state modal-info">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="modal-info-label"><i class="fa fa-info"></i></h4>
                            </div>
                            <div class="modal-body" style="min-height: 150px;">

                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-info" id="save" data-dismiss="modal">保存</button>
                                <button type="button" class="btn btn-default" id="return" data-dismiss="modal">返回</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="info-modal2" tabindex="-1" role="dialog" aria-labelledby="modal-info-label">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header state modal-info">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="modal-info-label"><i class="fa fa-info"></i></h4>
                            </div>
                            <div class="modal-body" style="min-height: 150px;">

                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-info" id="save2" data-dismiss="modal">保存</button>
                                <button type="button" class="btn btn-default" id="return2" data-dismiss="modal">返回</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<input type="hidden" name="save_channel" value="<?php xcho($saveChannel) ?>"/>
<input type="hidden" name="batch_edit_level" value="<?php xcho($batchEditLevel) ?>"/>
<input type="hidden" name="del_channel_info" value="<?php xcho($delChannelInfo) ?>"/>
<input type="hidden" name="change_channel" value="<?php xcho($changeChannel) ?>"/>
<input type="hidden" name="level_list" value="<?php xcho($levelList) ?>"/>
<input type="hidden" name="channel_detail" value="<?php xcho($channelDetail) ?>"/>
<script src="<?php xcho(loadSource('/assets/plugins/jquery/jquery.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/bootstrap/js/bootstrap.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/data-table/media/js/jquery.dataTables.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/data-table/media/js/dataTables.bootstrap.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/layer-v3.0.3/layer/layer.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/layui/layui.all.js')) ?>" charset="utf-8"></script>
<script
    src="<?php xcho(loadSource('/assets/plugins/data-table/extensions/Responsive/js/dataTables.responsive.min.js')) ?>"></script>
<script
    src="<?php xcho(loadSource('/assets/plugins/data-table/extensions/Responsive/js/responsive.bootstrap.min.js')) ?>"></script>
<script>
    $(function () {
        "use strict";
        layui.use('table', function(){
            var table = layui.table;
            var $ = layui.jquery,
                form = layui.form;
            form.on('checkbox(allChoose)', function(data){
                var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
                child.each(function(index, item){
                    item.checked = data.elem.checked;
                });
                form.render('checkbox');
            });
            form.on('checkbox(itemChoose)',function(data){
                var sib = $(data.elem).parents('table').find('tbody input[type="checkbox"]:checked').length;
                var total = $(data.elem).parents('table').find('tbody input[type="checkbox"]').length;
                if(sib == total){
                    $(data.elem).parents('table').find('thead input[type="checkbox"]').prop("checked",true);
                    form.render();
                }else{
                    $(data.elem).parents('table').find('thead input[type="checkbox"]').prop("checked",false);
                    form.render();
                }
            });
            $("#add").on("click", function () {
                var cn_id = $(this).attr("data-cn_id");
                $.ajax({
                    url: $("input[name='level_list']").val(),
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data)
                        if(data.success == 1){
                            var data_list = data.data_list;
                            var select_group = '<div class="form-group">' +
                                '<label for="hex-colorpicker" class="col-sm-2 control-label">渠道名称:</label>' +
                                '<div class="col-sm-10">' +
                                '<input type="text" name="dict_name" class="form-control" id="hex-colorpicker" value="">' +
                                '</div>' +
                                '</div>' +
                                '<div class="form-group">'+
                                '<label for="select2-example-basic" class="col-sm-2 control-label">渠道分级:</label>' +
                                '<div class="col-sm-10">' +
                                '<select name="level_select" id="select2-example-basic" class="form-control select2-hidden-accessible" style="width: 100%" tabindex="-1" aria-hidden="true">';
                            $.each(data_list,function(i,item){
                                select_group +=
                                    '<option value="'+item.dict_value+'" label="'+item.cn_name+'">'+item.cn_name+'</option>';
                            })
                            select_group += '</select></div>';
                            $('#info-modal .modal-body').html("").html(select_group);
                        }else{

                        }
                    },
                    complete:function(){
                        $('#info-modal').modal({
                            keyboard: false,
                            backdrop:false
                        });
                        $('#info-modal').on('shown.bs.modal', function (e) {
                            // 关键代码，如没将modal设置为 block，则$modala_dialog.height() 为零
                            $(this).find('.modal-dialog').css({
                                'margin-top': "100px"
                            });
                        });
                    },
                    error: function () {

                    }
                });

            });
            $(".edit").on("click", function () {
                var cn_id = $(this).attr("data-cn_id");
                $.ajax({
                    url: $("input[name='channel_detail']").val(),
                    data:{
                        id:cn_id
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data)
                        if(data.success == 1){
//                        var data_list = data.data_list;
                            var select_group = '<div class="form-group">' +
                                '<label for="hex-colorpicker" class="col-sm-2 control-label">渠道名称:</label>' +
                                '<div class="col-sm-10">' +
                                '<input type="hidden" name="cns_id" value="'+cn_id+'"/><input type="text" name="dict_name" class="form-control" id="hex-colorpicker" value="'+data.detail.cn_name+'">' +
                                '</div>' +
                                '<label for="select2-example-basic" class="col-sm-2 control-label">渠道分级:</label>' +
                                '<div class="col-sm-10">' +
                                '<select name="level_select" id="select2-example-basic" class="form-control select2-hidden-accessible" style="width: 100%" tabindex="-1" aria-hidden="true">';
                            $.each(data.level,function(i,item){
                                if(data.detail.level_value == item.dict_value){
                                    select_group +=
                                        '<option selected value="'+item.dict_value+'" label="'+item.cn_name+'">'+item.cn_name+'</option>';
                                }else {
                                    select_group +=
                                        '<option value="'+item.dict_value+'" label="'+item.cn_name+'">'+item.cn_name+'</option>';
                                }
                            })
                            select_group += '</select></div>';
                            $('#info-modal .modal-body').html("").html(select_group);
                            $('#info-modal').modal({
                                keyboard: false,
                                backdrop:false
                            });
                            $('#info-modal').on('shown.bs.modal', function (e) {
                                // 关键代码，如没将modal设置为 block，则$modala_dialog.height() 为零
                                $(this).find('.modal-dialog').css({
                                    'margin-top': "100px"
                                });
                            });
                        }else{

                        }
                    },
                    complete:function(){

                    },
                    error: function () {

                    }
                });

            });
            $(".delete").on("click",function(){
                var cn_id = $(this).attr("data-cn_id");
                layer.confirm('删除将无法恢复，确定删除吗？', {
                    skin: 'layui-layer-molv',
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url: $("input[name='del_channel_info']").val(),
                        data:{
                            id:cn_id
                        },
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            console.log(data)
                            if(data.success ==1){
                                layer.alert("删除成功!", {
                                    skin: 'layui-layer-molv'
                                }, function(){
                                    location.reload();
                                })
                            }else{
                                layer.alert(data.msg)
                            }
                        },
                        complete:function(){
                        },
                        error: function () {

                        }
                    });
                }, function(){
                });
            })
            function saveEdit(param){
                $.ajax({
                    url: $("input[name='save_channel']").val(),
                    data:param,
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data)
                        if(data.success == 1){
                            layer.alert("保存成功",function(){
                                location.reload();
                            })
                        }else{
                            layer.alert("未知系统错误,请刷新重试.")
                        }
                    },
                    complete:function(){
                    },
                    error: function () {

                    }
                });
            }
            $('#save').on("click",function(){
                var cn_name = $("input[name='dict_name']").val();
                var cn_id = $("input[name='cns_id']").val();
                if(cn_name != ""){
                    saveEdit({
                        id:cn_id,
                        cn_name:cn_name,
                        level_value:$("#select2-example-basic").val()
                    })
                }else{
                    alert("请填写渠道名称!");
                    return false;
                }

            });
            $("#change_levels").on("click", function () {
                var ids = [];
                $('input[name="ck"]:checked').each(function () {
                    ids.push($(this).val());
                });
                console.info(ids);
                if (ids.length == 0) {
                    layer.alert("至少选择一项", {
                        skin: 'layui-layer-molv'
                    });
                } else {
                    $.ajax({
                        url: $("input[name='level_list']").val(),
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            console.log(data)
                            if(data.success == 1){
                                var data_list = data.data_list;
                                var select_group = '<div class="form-group">' +
                                    '<label for="select2-example-basic" class="col-sm-2 control-label">渠道分级:</label>' +
                                    '<div class="col-sm-10">' +
                                    '<select name="level_select" id="select2-example-basic" class="form-control select2-hidden-accessible" style="width: 100%" tabindex="-1" aria-hidden="true">';
                                $.each(data_list,function(i,item){
                                    select_group +=
                                        '<option value="'+item.dict_value+'" label="'+item.cn_name+'">'+item.cn_name+'</option>';
                                })
                                select_group += '</select></div>';
                                $('#info-modal2 .modal-body').html("").html(select_group);
                            }else{

                            }
                        },
                        complete:function(){
                            $('#info-modal2').modal();
                            $('#save2').on("click",function(){
                                var cn_name = $("input[name='dict_name']").val();
                                if(cn_name != ""){
                                    saveChanges({
                                        ids:ids,
                                        level_value:$("#select2-example-basic").val()
                                    })
                                }else{
                                    layer.alert("请填写渠道名称!", {
                                        skin: 'layui-layer-molv'
                                    });
                                    return false;
                                }
                            });
                        },
                        error: function () {

                        }
                    });
                }

            });
            function saveChanges(params){
                $.ajax({
                    url: $("input[name='batch_edit_level']").val(),
                    data: params,
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data)
                        if(data.success == 1){
                            layer.alert("保存成功",function(){
                                location.reload();
                            })
                        }else{
                            alert("未知系统错误,请刷新重试.")
                        }
                    },
                    complete:function(){
                    },
                    error: function () {

                    }
                });
            }

        });
        //TABLE

    });
</script>
<?php include_once TPL_PATH . 'public/foot_script.phtml'; ?>

<button id="top" style="height: 50px;width: 50px;"></button></body>

</html>
